package com.xnyzc.lhy.mis.service.impl.cms.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.system.OaSysGlobalConfig;
import com.xnyzc.lhy.mis.mapper.system.OaSysGlobalConfigMapper;
import com.xnyzc.lhy.mis.service.cms.config.ICmsGlobalConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统配置
 * @Author conglijuan
 */
@Slf4j
@Service
public class CmsGlobalConfigServiceImpl implements ICmsGlobalConfigService {


    @Autowired
    private OaSysGlobalConfigMapper oaSysGlobalConfigMapper;


    /**
     * 获取系统配置信息
     * @return
     */
    @Override
    public Rv getCmsGlobalConfigDetail(){
        //查询系统配置
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaSysGlobalConfig.TAG,TokenUtil.getTag());
        List<OaSysGlobalConfig> oaSysGlobalConfigList=oaSysGlobalConfigMapper.selectList(queryWrapper);
        if(CheckUtil.objIsEmpty(oaSysGlobalConfigList)){
            throw PangException.create(EErrorCode.noData);
        }
        OaSysGlobalConfig oaSysGlobalConfig=oaSysGlobalConfigList.get(0);
        return Rv.wrap(oaSysGlobalConfig);
    }

    /**
     * 更新系统配置信息
     * @param oaSysGlobalConfig
     * @return
     */
    @Override
    public Rv updateCmsGlobalConfig(OaSysGlobalConfig oaSysGlobalConfig){
        //查询系统配置
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaSysGlobalConfig.TAG,TokenUtil.getTag());
        List<OaSysGlobalConfig> oaSysGlobalConfigList=oaSysGlobalConfigMapper.selectList(queryWrapper);
        Date date=new Date();
        Long createName=TokenUtil.getTokenUser().getTokenUserId();
        if(CheckUtil.objIsEmpty(oaSysGlobalConfigList)){
            //添加
            oaSysGlobalConfig.setUpdateUser(createName);
            oaSysGlobalConfig.setUpdateTime(date);
            oaSysGlobalConfig.setCreateTime(date);
            oaSysGlobalConfig.setCreateUser(createName);
            oaSysGlobalConfigMapper.insert(oaSysGlobalConfig);
            return Rv.wrap(EErrorCode.success);
        }
        //更新
        oaSysGlobalConfig.setConfigId(oaSysGlobalConfigList.get(0).getConfigId());
        oaSysGlobalConfig.setUpdateTime(date);
        oaSysGlobalConfig.setUpdateUser(createName);
        oaSysGlobalConfigMapper.updateById(oaSysGlobalConfig);
        return Rv.wrap(EErrorCode.success);
    }
}
